<template>
  <div class="">
    <com-form
        ref="form"
        :col="12"
        :configs="configs"
        labelWidth="95px"
    />
  </div>
</template>

<script>
import service from "@/service";
import {
    querySmsItemRender,
} from "@utils/formRender.js";

export default {
    name: 'sms',
    props: {
        phone: String,
        successHandle: Function
    },
    data() {
        return {
            temMap: {},// 短信模板map
            configs: [
                {
                    key: 'tmplId',
                    label: '短信模板',
                    col: 6,
                    required: true,
                    type: "render",
                    render: querySmsItemRender.apply(this, [
                        {
                            clearable: true,
                            className: "w-100",
                            nullQuerySign: true,
                            changeOptionsOut: (handle) => {},
                            onDataMapHandle: (v) => (this.temMap = v),
                            formChangeHandle: (vm, v) => {
                                vm.formData.smsContent = _.get(
                                    this,
                                    `temMap.${v}.content`
                                );
                            },
                        },
                    ]),
                },
                {
                    key: 'smsContent',
                    label: '短信内容',
                    col: 9,
                    props: {
                        type: 'textarea',
                        readonly: true,
                        row: 3
                    }
                },
                {
                    key: 'useless',
                    label: '',
                    noLabel: true,
                    type: 'render',
                    render: (h, vm, config, data, handle) => {
                        return <el-button type="primary" size="mini" on-click={this.submit}>发送</el-button>
                    }
                }
            ]
        }
    },
    created() {
        
    },
    watch: {
        phone(n) {
            this.$nextTick(() => {
                this.reset();
            })
        }
    },
    methods: {
        async submit() {
            let flag = await this.$refs.form.validate().catch(e => false)
            if(!flag) return;
            let formData = this.$refs.form.getHandleFormData();
            let params = {
                // smstemplateId: formData.tmplId, // 短信模板id
                noticeContent: formData.smsContent,
                mobile: this.phone, // 手机
                staffId: this.staffId, // 操作员工
                orgId: this.orgaId, // 组织id
            }
            let result = await service('msgTask/sendMsg', {
                params,
                noMessageTip: true,
            })
            
            if(result && result.result === '0') {
                this.$message.success("短信发送成功");
                if(this.successHandle) {
                    this.successHandle();
                }
            }else {
                this.$message.error(result?.RSP_DESC || result?.msg || '发送失败')
            }
        },
        reset() {
            this.$refs.form.resetFields()
        }
    }
}
</script>

<style>

</style>